// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class tableTouchMove extends cc.Component {

    pos1 = 0;

    pos2 = null;

    @property()
    boardLimit : cc.Vec2 = cc.v2(2000,2000);

    @property()
    isLine : boolean = true;

    onLoad () {
        this.node.on("touchstart",this.TouchStart,this);
        this.node.on("touchmove",this.TouchMove,this);
        // this.node.on("touchend",this.TouchEnd,this);
        // this.node.on("touchcancel",this.TouchEnd,this);
    }

    TouchStart(event){
        let worldPoint = event.getLocation();
        let pos = this.node.convertToNodeSpaceAR(worldPoint);
        if(this.isLine){
            this.pos1 = pos.x;
        }else{
            this.pos1 = pos.y;
        }

    }

    TouchMove(event){
        if(this.isLine){
            let worldPoint = event.getLocation();
            let pos = this.node.convertToNodeSpaceAR(worldPoint);
            if(this.node.x + (pos.x - this.pos1)  <= this.boardLimit.x || this.node.x + (pos.x - this.pos1) >= this.boardLimit.y){

            }else{
                this.node.x += (pos.x - this.pos1);
                this.pos1 = pos.x;
            }
        }else{
            let worldPoint = event.getLocation();
            let pos = this.node.convertToNodeSpaceAR(worldPoint);
            if(this.node.y + (pos.y - this.pos1)  <= this.boardLimit.x || this.node.y + (pos.y - this.pos1) >= this.boardLimit.y){

            }else{
                this.node.y += (pos.y - this.pos1);
                this.pos1 = pos.y;
            }
        }
    }

    // TouchEnd(event){
    //     let worldPoint = event.getLocation();
    //     let pos = this.node.convertToNodeSpaceAR(worldPoint);
    //     this.node.x = pos.x - this.pos1;
    //     this.pos1 = pos;
    // }
}
